using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace DIS_DAO.DAO
{
	#region using
	using System.Data;
	using DIS_DAO.DTO;
	using DIS_DAO.Helpers;
	#endregion

	/// <summary>
	/// Data access object, which get data of Orders table from database
	/// </summary>
	public class OrdersDAO : AbstractDAO
	{
		public OrdersDAO(string strPath): base(strPath)
		{
		}

		public DataSet SelectAll()
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Orders_SelectAll";
				base.FillDataSet(ref ds, "Orders");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByID(int id)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Orders_SelectByID";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				base.FillDataSet(ref ds, "Orders");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public bool Insert(ref OrdersDTO obj)
		{
            bool kq;
			try
			{
                if (obj.IDCustomer == 0)
                {
                    base.sql = "usp_Orders_InsertWithoutCustomer";
                    base.InitializeCommand();
                }
                else
                {
                    base.sql = "usp_Orders_Insert";
                    base.InitializeCommand();
                    base.AddParameter("@IDCustomer", SqlDbType.Int, 1000, obj.IDCustomer);
                }

				base.AddParameter("@IDChemistWareHouse", SqlDbType.Int, 1000, obj.IDChemistWareHouse);
				base.AddParameter("@OrderDate", SqlDbType.DateTime, 1000, obj.OrderDate);
				base.AddParameter("@IsVerified", SqlDbType.Bit, 1000, obj.IsVerified);
				base.AddParameter("@IsShipped", SqlDbType.Bit, 1000, obj.IsShipped);
				base.AddParameter("@IsPaid", SqlDbType.Bit, 1000, obj.IsPaid);
                base.AddParameter("@AddressOfReceiver", SqlDbType.NVarChar, 1000, obj.AddressOfReceiver);
                base.AddParameter("@NameOfReceiver", SqlDbType.NVarChar, 1000, obj.NameOfReceiver);
                base.AddParameter("@PhoneOfReceiver", SqlDbType.NVarChar, 1000, obj.PhoneOfReceiver);

                base.AddParameterWithDirection("@ID", SqlDbType.Int, 1000);

                kq = ExecuteStoredProcedure();
                obj.ID = int.Parse(base.GetParamValue("@ID").ToString());

                return kq;
			}
			catch
			{
				return false;
			}
		}

		public bool Update(OrdersDTO obj)
		{
			try
			{
                if (obj.IDCustomer == 0)
                {
                    base.sql = "usp_Orders_UpdateWithoutCustomer";
                    base.InitializeCommand();
                }
                else
                {
                    base.sql = "usp_Orders_Update";
                    base.InitializeCommand();
                    base.AddParameter("@IDCustomer", SqlDbType.Int, 1000, obj.IDCustomer);
                }

				base.AddParameter("@ID", SqlDbType.Int, 1000, obj.ID);
				base.AddParameter("@IDChemistWareHouse", SqlDbType.Int, 1000, obj.IDChemistWareHouse);
				base.AddParameter("@OrderDate", SqlDbType.DateTime, 1000, obj.OrderDate);
				base.AddParameter("@IsVerified", SqlDbType.Bit, 1000, obj.IsVerified);
				base.AddParameter("@IsShipped", SqlDbType.Bit, 1000, obj.IsShipped);
				base.AddParameter("@IsPaid", SqlDbType.Bit, 1000, obj.IsPaid);
                base.AddParameter("@AddressOfReceiver", SqlDbType.NVarChar, 1000, obj.AddressOfReceiver);
                base.AddParameter("@NameOfReceiver", SqlDbType.NVarChar, 1000, obj.NameOfReceiver);
                base.AddParameter("@PhoneOfReceiver", SqlDbType.NVarChar, 1000, obj.PhoneOfReceiver);

				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				base.sql = "usp_Orders_Delete";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public DataSet SelectByIDChemistWareHouse(int idchemistwarehouse)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Orders_SelectByIDChemistWareHouse";
				base.InitializeCommand();
				base.AddParameter("@IDChemistWareHouse", SqlDbType.Int, 1000, idchemistwarehouse);
				base.FillDataSet(ref ds, "Orders");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByIDCustomer(int idcustomer)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Orders_SelectByIDCustomer";
				base.InitializeCommand();
				base.AddParameter("@IDCustomer", SqlDbType.Int, 1000, idcustomer);
				base.FillDataSet(ref ds, "Orders");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

        public DataSet SelectByIDChemistWareHouseAndOrderDate(int idChemistWareHouse, DateTime orderdate)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Orders_SelectByIDChemistWareHouseAndOrderDate";
                base.InitializeCommand();
                base.AddParameter("@IDChemistWareHouse", SqlDbType.Int, 1000, idChemistWareHouse);
                base.AddParameter("@OrderDate", SqlDbType.DateTime, 1000, orderdate);
                base.FillDataSet(ref ds, "Orders");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }
	}
}